<?php 
class ConfigModel extends Model{
	public $table='config';

	public function updateConfig(){
		foreach ($_POST as $key => $value) {
			// p($_POST);exit;
			$data['value']=$value;
			// p($data);
			if(!$this->where("name='$key'")->save($data)){
				$this->error='修改失败';
				return false;
			}
		}
		return $this->createConfigFile();
	}

	public function createConfigFile(){
		$config=$this->getField('name,value');
		$data="<?php \nreturn ".var_export($config,true).";\n?>";
		return file_put_contents(APP_CONFIG_PATH.'base.php', $data);
	}

	public function get(){
		$config=$this->all();
		// p($config);exit;
		foreach ($config as $key => $value) {
			$func='_'.$value['type'];
			$config[$key]['_html']=$this->$func($value);
		}
		return $config;
	}

	public function setTplStyle(){
		$dir=Q('dir');
		$status=M('config')->where("name='webstyle'")->save(array('value'=>$dir));
		return $this->createConfigFile();
	}

	public function _text($config){
		return "<input type='text' name='{$config['name']}' value='{$config['value']}'>";
	}

	public function _textarea($config){
	// <textarea name="" id="" cols="30" rows="10"></textarea>
		return "<textarea name='{$config['name']}'>{$config['value']}</textarea>";
	}

	public function _radio($config){					
		$radio=explode(',',$config['info']);
		$html='';
		foreach ($radio as $key => $r) {
			$info=explode('|',$r);
			// p($info);exit;			
			$checked=$info[0]==$config['value']?'checked=""':'';
			$html.="<label><input type='radio' $checked name='{$config['name']}' value='{$info[0]}' >$info[1]</label>";
		}
		return $html;
	}
}

?>